package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.pm.dto.UserQuestionnaireAddDTO;
import com.oa.pm.entity.UserQuestionnaire;
import com.oa.pm.service.IUserQuestionnaireService;
import com.oa.pm.vo.TopicDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/userQuestionnaire")
@Api(description = "用户填写问卷表模块")
public class UserQuestionnaireController extends BaseController {

    private final IUserQuestionnaireService userQuestionnaireService;

    @Autowired
    public UserQuestionnaireController(IUserQuestionnaireService userQuestionnaireService) {
        this.userQuestionnaireService = userQuestionnaireService;
    }

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param title
     * @param id
     * @return
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp<List<UserQuestionnaire>> list(
            @ApiParam("页号") @RequestParam("pageNum") Integer pageNum,
            @ApiParam("行数") @RequestParam("pageSize") Integer pageSize,
            @ApiParam("标题") @RequestParam(value = "title", required = false) String title,
            @ApiParam("ID") @RequestParam(value = "id", required = false) String id) {
        return new Resp().success(userQuestionnaireService.findUserQuestionnaireList(pageNum, pageSize, title, id));
    }

    @GetMapping("/me/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp<List<UserQuestionnaire>> melist(
            @ApiParam("页号") @RequestParam("pageNum") Integer pageNum,
            @ApiParam("行数") @RequestParam("pageSize") Integer pageSize,
            @ApiParam("标题") @RequestParam(value = "title", required = false) String title) {
        return new Resp().success(userQuestionnaireService.findMyUserQuestionnaireList(pageNum, pageSize, title, getLoginUserId()));
    }



    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/questionnaire/{id}")
    @CommonController
    @Description("查询题目")
    @ApiOperation(value = "查询题目")
    public Resp<TopicDetailsVO> findTopicByQuestionnaireId(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(userQuestionnaireService.findTopicByQuestionnaireId(id));
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("查询题目")
    @ApiOperation(value = "查询题目")
    public Resp findAnswer(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(userQuestionnaireService.findAnswer(id, getLoginUserId()));
    }


    /**
     * 新增
     *
     * @param userQuestionnaireAddDTO UserQuestionnaireAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "填写")
    public Resp add(@ApiParam(value = "用户填写问卷表对象") @RequestBody(required = false) UserQuestionnaireAddDTO userQuestionnaireAddDTO) {
        userQuestionnaireAddDTO.setUserId(getLoginUserId());
        if (userQuestionnaireService.add(userQuestionnaireAddDTO)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }
}
